#! /bin/bash

APP=ycreal
sqoop=/opt/module_no_kerberos/sqoop-1.4.6/bin/sqoop


starttime=`date +'%Y-%m-%d %H:%M:%S'`


if [ -n "$2" ] ;then
    string=$2
else
    string=`date +'%Y-%m-%d %H:%M:%S'`
fi 
 
# '2018-09-01 00:15:00' 
array=(`echo $string | tr ' ' ' '` )  

flag=0 
 
for var in ${array[@]}
do
    if [ ${flag} -eq 0 ]; then
		day=$var
	fi
   time=$var
   flag=1
done 


string=$time 
array=(`echo $string | tr ':' ' '` )


hour=${time:0:2}
min=${time:3:2}

echo $day
echo $hour
echo "old" $min

thisString="01 02 03 04 05 06 07 08 09 10 11 12 13 14" # 源字符串
searchString=${min} # 搜索字符串
case $thisString in
	*"$searchString"*) echo "包含(01~15)" 
	min='00'
	;;
	*) echo "不包含(01~15)" 
	;;
esac

thisString="16 17 18 19 20 21 22 23 24 25 26 27 28 29" # 源字符串
searchString=${min} # 搜索字符串
case $thisString in
	*"$searchString"*) echo "包含(16~30)" 
	min='15'
	;;
	*) echo "不包含(16~30)" 
	;;
esac

thisString="31 32 33 34 35 36 37 38 39 40 41 42 43 44" # 源字符串
searchString=${min} # 搜索字符串
case $thisString in
	*"$searchString"*) echo "包含(31~45)" 
	min='30'
	;;
	*) echo "不包含(31~45)" 
	;;
esac

thisString="46 47 48 49 50 51 52 53 54 55 56 57 58 59" # 源字符串
searchString=${min} # 搜索字符串
case $thisString in
	*"$searchString"*) echo "包含(46~59)" 
	min='45'
	;;
	*) echo "不包含(46~59)" 
	;;
esac

echo $day
echo $hour
echo "new" $min

# 在处理完时间的基础上，进行时间操作

no1=1
no24=24
no15=15
do_day=$day
no31=31
no30=30


if [ ${min} -eq '00' ]; then

	thisString="01 02 03 04 05 06 07 08 09" # 源字符串
	searchString=${hour} # 搜索字符串
	case $thisString in
		*"$searchString"*) echo "包含1" 
		hour=$(echo -e $searchString | sed -r 's/0*([0-9])/\1/')
		;;
		*) echo "不包含2" 
		;;
	esac
	#echo $hour

	let do_time_hour=$hour-no1
	do_time_min='45'
	
else
	let do_time_min=$min-no15
	
	thisString="0 1 2 3 4 5 6 7 8 9" # 源字符串
	searchString=${do_time_min} # 搜索字符串
	case $thisString in
		*"$searchString"*) echo "包含3" 
		do_time_min="0$do_time_min";;
		*) echo "不包含3" ;;
	esac
fi

if [ ${hour} -eq '00' ] && [ ${min} -eq '00' ]; then
	let do_time_hour=$no24-no1
	do_time_min='45'
	
	echo "进入0点处理"
	
	string=$do_day 
	array=(`echo $string | tr '-' ' '` )

	do_day_year=${do_day:0:4}
	do_day_month=${do_day:5:2}	
	do_day_day=${do_day:8:2}	
	
	if [ ${do_day_day} -eq '01' ]; then
	
		thisString="01 02 03 04 05 06 07 08 09" # 源字符串
		searchString=${do_day_month} # 搜索字符串
		case $thisString in
			*"$searchString"*) echo "包含6" 
			day_month=$(echo -e $searchString | sed -r 's/0*([0-9])/\1/')
			let do_day_month=$day_month-no1	
			
			echo $do_day_month
			
			
			thisString="0 1 2 3 4 5 6 7 8 9" # 源字符串
			searchString=${do_day_month} # 搜索字符串
			case $thisString in
				*"$searchString"*) echo "包含7" 
				do_day_month="0$do_day_month"
				
				
					thisString="01 03 05 07 08" # 源字符串
					searchString=${do_day_month} # 搜索字符串
					case $thisString in
					*"$searchString"*) echo "包含8" 
					do_day_day=$no31
					
					;;
					*) echo "不包含8"
						do_day_day=$no30
					;;
					esac			
				;;
				*) echo "不包含7"
				
				;;
			esac		
			;;
			*) echo "不包含6" 
			if [ ${do_day_month} -eq '10' ]; then
	
				let do_day_month=$do_day_month-no1	
				do_day_month="0$do_day_month"
				do_day_day=$no30
				
			else
				let do_day_month=$do_day_month-no1	
				
				if [ ${do_day_month} -eq '10' ]; then
					do_day_day=$no31					
				else
					do_day_day=$no30						
				fi
				
			fi
			;;
		esac
		
	else
		thisString="01 02 03 04 05 06 07 08 09" # 源字符串
		searchString=${do_day_day} # 搜索字符串
		case $thisString in
			*"$searchString"*) echo "包含4" 
			day_day=$(echo -e $searchString | sed -r 's/0*([0-9])/\1/')
			let do_day_day=$day_day-no1	
			
			thisString="0 1 2 3 4 5 6 7 8 9" # 源字符串
			searchString=${do_day_day} # 搜索字符串
			case $thisString in
				*"$searchString"*) echo "包含5" 
				do_day_day="0$do_day_day";;
				*) echo "不包含5" ;;
			esac		
			;;
			*) echo "不包含4" 
			let do_day_day=$do_day_day-no1
			;;
		esac
	fi	
		
	do_day="$do_day_year-$do_day_month-$do_day_day"

	echo "0点处理结束"	
fi

if [ ${min} -eq '00' ]; then
	thisString="0 1 2 3 4 5 6 7 8 9" # 源字符串
	searchString=${do_time_hour} # 搜索字符串
	case $thisString in
		*"$searchString"*) echo "包含2" 
		do_time_hour="0$do_time_hour";;
		*) echo "不包含2" ;;
	esac
else
	do_time_hour=$hour
fi

#echo $do_time_hour


do_time_start="$do_day $do_time_hour:$do_time_min:00"
do_time="$day $hour:$min:00"

echo $do_time_start
echo $do_time


import_data(){
$sqoop import \
--connect jdbc:mysql://hadoop102:3306/$APP \
--username root \
--password 123456 \
--target-dir /origin_water_data/$APP/db/$1/$day/$hour/$min \
--delete-target-dir \
--query "$2 and  \$CONDITIONS" \
--num-mappers 1 \
--fields-terminated-by '\t' \
--compress \
--compression-codec lzop \
--null-string '\\N' \
--null-non-string '\\N'

hadoop jar /opt/module_no_kerberos/hadoop-3.1.3/share/hadoop/common/hadoop-lzo-0.4.20.jar com.hadoop.compression.lzo.DistributedLzoIndexer /origin_water_data/$APP/db/$1/$day/$hour/$min
}


import_st_grid_point(){
  import_data st_grid_point "select
                              id,
                              x,
                              y
                            from st_grid_point
                            where 1=1"
}


import_st_stbprp_b(){
  import_data st_stbprp_b "select
                              STCD,
                              LGTD,
                              LTTD
                            from st_stbprp_b
                            where 1=1"
}


import_st_pptn_r(){
    import_data st_pptn_r "select
                              INSERTTIME,                            
                              STCD,
                              DRP
                          from st_pptn_r
                          where st_pptn_r.INSERTTIME BETWEEN STR_TO_DATE('$do_time_start','%Y-%m-%d %H:%i:%s') AND STR_TO_DATE('$do_time','%Y-%m-%d %H:%i:%s')"
}



case $1 in
  "st_grid_point")
     import_st_grid_point
;;
  "st_stbprp_b")
     import_st_stbprp_b
;;
  "st_pptn_r")
     import_st_pptn_r
;;
"all")
   #import_st_grid_point
   #import_st_stbprp_b
   import_st_pptn_r
;;
esac

endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s);
end_seconds=$(date --date="$endtime" +%s);
echo "本次运行时间: "$((end_seconds-start_seconds))"s"




